package com.joinway.framework.core.repository;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.joinway.framework.core.bean.domain.SystemConfig;
import com.joinway.framework.core.utils.AppEnv;

@Service
public class SystemConfigRepository {

	public static final String AndroidPushKey = "admin.push.android.appKey";
	public static final String AndroidPushMasterKey = "admin.push.android.masterSecret";
	
	public static final String IosPushKey = "admin.push.ios.appKey";
	public static final String IosPushMasterKey = "admin.push.ios.masterSecret";
	
	public static final String FileDir = "admin.file.dir";
	
	public static final String PageSuffix = "admin.page.suffix";
	public static final String PageRequestSuffix = "admin.page.request.suffix";
	
	public static final String PushLiveDays = "admin.push.liveDays";
	public static final String PushIosProduction = "admin.push.ios.production";
	
	public static final String JspRoot = "admin.jsp.root";
	
	public static final String ClientAndroidVersion = "client.android.version";
	public static final String ClientAndroidUrl = "client.android.url";
	public static final String ClientAndroidUpgrade = "client.android.upgrade";
	
	public static final String ClientIosVersion = "client.ios.version";
	public static final String ClientIosUrl = "client.ios.url";
	public static final String ClientIosUpgrade = "client.ios.upgrade";
	
	public static final String WebRootPath = "web.root.path";
	
	public static final String ExcelColumnWithDelt = "admin.excel.column.width.delt";
	public static final String ExcelColumnMaxWidth = "admin.excel.column.width.max";

	public static final String UEditorPath = "admin.ueditor.path";
	
	public static final String AdminSiteName = "admin.site.name";
	
	public static final String AdminHostName = "admin.host.name";
	public static final String AdminHostPort = "admin.host.port";
	
	@Autowired FrameworkRepository repository;
	
//	/**
//	 * 从数据库中获得系统配置
//	 * @param key	配置key
//	 * @param appSensitive	是否检查appid
//	 * @return
//	 * @throws Exception
//	 */
//	public String getStringValue(String key, boolean appSensitive) throws Exception {
//		String value = System.getProperty(key);
//		
//		if(StringUtils.isBlank(value)){
//			List<SystemConfig> configs = repository.getSystemConfig();
//	
//			if (CollectionUtils.isNotEmpty(configs)) {
//				for (SystemConfig config : configs) {
//					if((appSensitive && StringUtils.equals(AppEnv.getAppId(), config.getAppId()) || !appSensitive) 
//							&& StringUtils.trimToEmpty(config.getName()).equals(StringUtils.trimToEmpty(key))){
//						value = config.getValue();
//						break;
//					}
//				}
//			}
//		}
//		
//		return value;		
//	}
	
	/**
	 * 从数据库中获得系统配置
	 * @param key	配置key
	 * @param appIds	目标appid
	 * @return
	 * @throws Exception
	 */
	public String getStringValue(String key, String...appIds) throws Exception {
		String value = System.getProperty(key);
		List<String> list = ArrayUtils.isNotEmpty(appIds) ? Arrays.asList(appIds) : null;
		
		if(StringUtils.isBlank(value)){
			List<SystemConfig> configs = repository.getSystemConfig();
	
			if (CollectionUtils.isNotEmpty(configs)) {
				for (SystemConfig config : configs) {
					if((CollectionUtils.isNotEmpty(list) && list.contains(config.getAppId()) || CollectionUtils.isEmpty(list)) 
							&& StringUtils.trimToEmpty(config.getName()).equals(StringUtils.trimToEmpty(key))){
						value = config.getValue();
						break;
					}
				}
			}
		}
		
		return value;			
	}
	
	/**
	 * get string value by current appid
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public String getStringValue(String key) throws Exception {
		return getStringValue(key, AppEnv.getAppId());
	}
	
	public boolean getBoolValue(String key) throws Exception {
		return getBoolValue(key, AppEnv.getAppId());
	}
	
	public boolean getBoolValue(String key, String...appIds) throws Exception {
		String value = getStringValue(key, appIds);
		
		return StringUtils.isBlank(value) ? false : Boolean.valueOf(value);
	}
	
	public long getLongValue(String key) throws Exception {
		return getLongValue(key, AppEnv.getAppId());
	}
	
	public long getLongValue(String key, String...appIds) throws Exception {
		String value = getStringValue(key, appIds);
		
		return StringUtils.isBlank(value) ? 0 : Long.valueOf(value);
	}
	
	public int getIntValue(String key) throws Exception {
		return getIntValue(key, AppEnv.getAppId());
	}
	
	public int getIntValue(String key, String...appIds) throws Exception {
		String value = getStringValue(key, appIds);
		
		return StringUtils.isBlank(value) ? 0 : Integer.valueOf(value);
	}
}
